<?php
class ApiOutput {
    public $error;
    public $data;

    public function __construct($shortCut, $data = null) {
        $list = $this->getList();
        foreach($list as $k => $v) {
            if($k == $shortCut) {
                $this->error = $v; break;
            }
            $this->error = $list['error_not_found'];
        }
        $this->data = $data;
    }

    public function getList() {
        $list['system_ok'] = array('code' => '10000', 'description' => '系统运行正常');
        $list['error_not_found'] = array('code' => '10001', 'description' => '你的错误消失在二次元');
        $list['lack_parameter'] = array('code' => '10002', 'description' => '缺少参数');
        $list['regex_error'] = array('code' => '10003', 'description' => '格式错误');
        $list['enum_error'] = array('code' => '10004', 'description' => '不在可选值列表中');

        //用户模块
        $list['nickname_exist'] = array('code' => '10100', 'description' => '昵称已存在');
        $list['phone_exist'] = array('code' => '10101', 'description' => '手机已存在');
        $list['email_exist'] = array('code' => '10102', 'description' => '邮箱已存在');

        return $list;
    }

    public function __toString() {
        if(!is_null($this->data)) {
            $this->error['data'] = $this->data;
        }
        return json_encode($this->error);
    }
}

